package com.zx.stock.thread;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zx.stock.common.Const;
import com.zx.stock.entity.Performance;
import com.zx.stock.entity.PerformanceData;
import com.zx.stock.mapper.PerformanceDataMapper;
import com.zx.stock.mapper.PerformanceMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@Slf4j
public class PerformanceDataThread implements Runnable{

    PerformanceData entity;
    PerformanceDataMapper performanceDataMapper;
    PerformanceMapper performanceMapper;

    public PerformanceDataThread(PerformanceData stockList, PerformanceDataMapper stockAgencyMapper,PerformanceMapper performanceMapper){
        this.entity = stockList;
        this.performanceDataMapper = stockAgencyMapper;
        this.performanceMapper=performanceMapper;
    }

    @Override
    public void run(){



        try {
            List<Performance> list = JSON.parseArray(entity.getContent(),Performance.class);
            if (list == null || list.isEmpty())return;

            if (performanceMapper.insertBatch(list)>0){

                list.forEach(k->k.setCtime(new Date()));

                log.info("新增成功:{}",entity.getId());
                PerformanceData item = new PerformanceData();
                item.setStatus(Const.F);
                item.setId(entity.getId());
                if(performanceDataMapper.updateDynamic(item)>0){
                    log.info("修改状态成功:{}",entity.getId());
                }else{
                    log.error("修改状态失败:{}",entity.getId());
                }
            }else{
                log.error("新增失败:{}",entity.getId());
            }


        }catch (Exception ex){
            ex.printStackTrace();
            log.error("解析数据失败:{}",entity.getId());
        }


    }
}
